import Vue from 'vue'
import axios from 'axios'
import config from './config.js'
let qs = require('qs');
import '../vendor/js/jssdk.js'
import '../vendor/js/common.js'
let JLBWEBVersion='3.1.0';
let webapiHost, host, domains, uri = location.host;
if (uri.indexOf('test') > -1) {
    webapiHost = 'https://test-web-api.bestjlb.com/';//验证
    host = 'https://test-authen.bestjlb.com/';//登录
    domains = 'https://test-html.bestjlb.com/';//api接口
} else if (uri.indexOf('bestjlb') > -1) {
    webapiHost = 'https://web-api.bestjlb.com/';//验证
    host = 'https://authen.bestjlb.com/';//登录
    domains = 'https://html.bestjlb.com/';//api接口
} else {
    webapiHost = 'http://101.69.254.162:8101/';
    host = 'http://101.69.254.162:8100/';
    domains = 'http://10.10.10.22:8001/jlb-web-front-app/';
}
export const imagedomain = "https://images.bestjlb.com/";
export const domain = domains;


class API {
    /***********************公共**********************************/
    //获取用户信息
    getUserInfo(param) {
        return axios(domain + 'jlbapp/user/getSelfInfo.shtml', {
            params: param
        })
    }

    //获取登录验证token
    getToken(param) {
        return axios.post(host + 'login', param);
    }
    //获取应用介绍信息
    getIntroduce(param){
        return axios(domain + 'jlbapp/introduce/selectIntroduces.shtml', {
            params: param
        })
    }
    //评论
    setComment(param) {
        return axios.post(domain + 'jlbapp/parts/comment/save.shtml', qs.stringify(param));
    }

    //一键评论
    setAllComment(param) {
        return axios.post(domain + 'jlbapp/parts/comment/saveBatch.shtml', qs.stringify(param));
    }

    //删除评论
    delComment(param) {
        return axios.get(domain + 'jlbapp/parts/comment/delete.shtml', {
            params: param
        });
    }

    //点赞
    clickZan(param) {
        return axios.get(domain + 'jlbapp/parts/focus/add.shtml', {
            params: param
        });
    }

    //取消点赞
    cancelZan(param) {
        return axios.get(domain + 'jlbapp/parts/focus/delete.shtml', {
            params: param
        });
    }

    //获取分享信息
    getShareInfo(param) {
        return axios.get(domain + 'class/getShareHead.shtml', {
            params: param
        });
    }

    //获取学生列表
    getGroupStudent(param) {
        return axios.get(domain + 'class/findGroupStudent.shtml', {
            params: param
        });
    }

    /***********************作业**********************************/
    //详情获取
    getHomeworkDetail(param) {
        return axios(domain + 'jlbapp/homework/v200/detail.shtml', {
            params: param
        })
    }

    //查询用户作业回复详情
    getHomeworkStudentDetail(param) {
        return axios(domain + 'jlbapp/homework/v200/findReplyDetail.shtml', {
            params: param
        })
    }

    //发送作业
    sendHomework(param) {
        return axios.post(domain + 'jlbapp/homework/tch_mng/saveV110.shtml', qs.stringify(param));
    }

    //获取单个学生作业详情
    getSingleHomework(param) {
        config.params = param;
        return axios.post(domain + 'jlbapp/homework/v200/findReplyDetail.shtml', {}, config);
    }

    //回复作业
    submitHomework(param) {
        return axios.post(domain + 'jlbapp/homework/v200/submitWork.shtml', qs.stringify(param));
    }

    //设置作业是否公开
    setHomeworkPub(param) {
        config.params = param;
        return axios.post(domain + 'jlbapp/homework/v200/setPub.shtml', {}, config);
    }

    /***********************课堂风采**********************************/
    //保存风采
    sendVideo(param) {
        return axios.post(domain + 'jlbapp/vedio/tch_mng/saveV200.shtml', qs.stringify(param));
    }

    //详情获取
    getVideoDetail(param) {
        return axios(domain + 'jlbapp/vedio/detailV200.shtml', {
            params: param
        })
    }

    //点赞或取消点赞
    changePraise(param) {
        return axios.get(domain + 'jlbapp/vedio/praiseOrCancle.shtml', {
            params: param
        });
    }

    //课堂风采认领
    setClaim(param) {
        return axios.get(domain + 'jlbapp/vedio/claimPic.shtml', {
            params: param
        });
    }

    //课堂风采取消认领
    cancelClaim(param) {
        return axios.get(domain + 'jlbapp/vedio/cancelClaim.shtml', {
            params: param
        });
    }

    /***********************班级通知**********************************/
    //班级通知保存
    sendNotice(param) {
        return axios.post(domain + 'jlbapp/notice/tch_mng/save.shtml', qs.stringify(param));
    }

    //班级通知详情
    getNoticeDetail(param) {
        return axios.get(domain + 'jlbapp/notice/v200/detail.shtml', {
            params: param
        });
    }

    //设置已读状态
    setHaveRead(param) {
        return axios.get(domain + 'jlbapp/notice/iHaveRead.shtml', {
            params: param
        });
    }

    /***********************今日作品**********************************/
    //获取上传学生列表
    getUploadStuList(param) {
        return axios.get(domain + 'jlbapp/opus/tch_mng/edit.shtml', {
            params: param
        });
    }

    //发送今日作品
    sendOpus(param) {
        return axios.post(domain + 'jlbapp/opus/tch_mng/send.shtml', qs.stringify(param));
    }

    //编辑单个学员今日作品
    editPersonOpus(param) {
        return axios.get(domain + 'jlbapp/opus/tch_mng/detail.shtml', {
            params: param
        });
    }

    //上传单个学员今日作品
    uploadPersonOpus(param) {
        return axios.post(domain + 'jlbapp/opus/tch_mng/submit.shtml', qs.stringify(param));
    }

    //删除某个学员今日作品
    deletePersonOpus(param) {
        return axios.get(domain + 'jlbapp/opus/tch_mng/delete.shtml', {
            params: param
        });
    }

    //今日作品详情
    getOpusDetail(param) {
        return axios.get(domain + 'jlbapp/opus/v200/detail.shtml', {
            params: param
        });
    }

    //老师查看今日作品学员列表
    getOpusDetailList(param) {
        return axios.get(domain + 'jlbapp/opus/tch_mng/view.shtml', {
            params: param
        });
    }

    /***********************课堂精华**********************************/
    //课堂精华保存
    sendClassRoom(param) {
        return axios.post(domain + 'jlbapp/apCore/select/tch_mng/saveV200.shtml', qs.stringify(param));
    }

    //课堂精华详情
    getClassRoomDetail(param) {
        return axios.get(domain + 'jlbapp/apCore/select/detailV200.shtml', {
            params: param
        });
    }

    /***********************成长日记**********************************/
    //发布成长日记
    sendGrowth(param) {
        return axios.post(domain + 'jlbapp/growthNotes/save.shtml', qs.stringify(param));
    }

    //查询目标标签
    getTarget(param) {
        config.params = param;
        return axios.post(domain + 'jlbapp/growthNotes/findTargets.shtml', {}, config);
    }

    //添加目标标签
    addTarget(param) {
        config.params = param;
        return axios.post(domain + 'jlbapp/growthNotes/addTarget.shtml', {}, config);
    }

    //删除目标标签
    delTarget(param) {
        config.params = param;
        return axios.post(domain + 'jlbapp/growthNotes/delTargets.shtml', {}, config);
    }

    //查看详情
    getGrowthDetail(param) {
        return axios.get(domain + 'jlbapp/growthNotes/detail.shtml', {
            params: param
        });
    }

    /************************打卡作业*********************************/
    //保存打卡作业
    sendMultiwork(param) {
        return axios.post(domain + 'jlbapp/multihomework/tch/saveV200.shtml', qs.stringify(param));
    }
    //提交打卡作业
    submitMultiwork(param) {
        return axios.post(domain + 'jlbapp/multihomework/saveOrUpdateMultiWork.shtml', qs.stringify(param));
    }
    //提交查询打卡作业提交内容
    searchStudentMultiwork(param) {
        return axios.post(domain + 'jlbapp/multihomework/updateSingleMultiWork.shtml', qs.stringify(param));
    }
    //删除打卡作业提交内容
    delStudentMultiwork(param) {
        return axios.post(domain + 'jlbapp/multihomework/deleteSingleMultiWork.shtml', qs.stringify(param));
    }
    //获取打卡作业主体详情数据
    getMultiDetail(param) {
        return axios.get(domain + 'jlbapp/apCore/select/findCore.shtml', {
            params: param
        });
    }

    //获取打卡作业时间轴
    getMultiDateList(param) {
        return axios.get(domain + 'jlbapp/multihomework/findSubmitDaysForMultiWorkV200.shtml', {
            params: param
        });
    }

    //获取回复未回复人员
    getMultiPersonList(param) {
        return axios.get(domain + 'jlbapp/apCore/select/findPersonnelForReply.shtml', {
            params: param
        });
    }

    //获取所有人作业信息
    getMultiReplyList(param) {
        return axios.get(domain + 'jlbapp/multihomework/findReplyDetailV200.shtml', {
            params: param
        });
    }

    //设置打卡作业公开权限
    setMultiPub(param) {
        return axios.get(domain + 'jlbapp/multihomework/setPub.shtml', {
            params: param
        });
    }
    //
    getStudentMultiwork(param) {
        return axios.get(domain + 'jlbapp/multihomework/findMultiWorkReplyDetail.shtml', {
            params: param
        });
    }
}
// 请求拦截
axios.interceptors.request.use(function (config) {
    config.headers['JLBWEBVersion'] = JLBWEBVersion;
    return Vue.prototype.$util.getToken().then(() => {
        if (sessionStorage.getItem('access_token')) {
            config.headers['access_token'] = sessionStorage.getItem('access_token');
            config.headers['Authorization'] = sessionStorage.getItem('token_type') + sessionStorage.getItem('access_token');
        }
        return config
    }, () => {
        if(sessionStorage.getItem('access_token')){
            config.headers['access_token'] = sessionStorage.getItem('access_token');
            config.headers['Authorization'] = sessionStorage.getItem('token_type') + sessionStorage.getItem('access_token');
        }else{
            config.headers['access_token'] = Uri.query().get('access_token');
            config.headers['Authorization'] = 'bearer' + Uri.query().get('access_token');
        }
        return config
    });
}, function (error) {
    console.log('验证失败');
    return Promise.reject(error);
});
// 响应拦截
axios.interceptors.response.use(function (response) {
    response = response.data;
    if (Object.prototype.toString.call(response) === '[object String]') {
        response = JSON.parse(response);
    }
    if (response.code !== 200 && localStorage.getItem('access_token')) {
        Vue.prototype.$toast(response.msg);
    }
    return response;
}, function (error) {
    Vue.prototype.$toast('网络连接失败,请重试');
    return Promise.reject(error);
});
export default API;

